package tech.lq0.dreamaticvoyage.voyage.core;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;

import java.util.ArrayList;
import java.util.List;

public class VoyageResult {

    public static final VoyageResult EMPTY = new VoyageResult(
            new Builder()
                    .translatableKey("test.dmv.test")
                    .successCondition(new float[]{Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE})
                    .weight(0)
                    .attributeBonus(new float[]{0, 0, 0, 0})
    );

    public final String translatableKey;
    public final ResourceLocation lootTable;
    public final float[] successCondition;
    public final int weight;
    // 事件结果类型
    public final ResultType resultType;
    // 完成事件时的属性加成
    public final float[] attributeBonus;

    public VoyageResult(Builder builder) {
        this.translatableKey = builder.translatableKey;
        this.lootTable = builder.lootTable;
        this.successCondition = builder.successCondition;
        this.weight = builder.weight;
        this.resultType = builder.resultType;
        this.attributeBonus = builder.attributeBonus;
    }

    public enum ResultType {
        CONTINUE,
        STOP,
    }

    public static class Builder {

        String translatableKey;
        ResourceLocation lootTable;
        float[] successCondition;
        int weight;
        ResultType resultType = ResultType.CONTINUE;
        float[] attributeBonus = new float[]{0, 0, 0, 0};

        public Builder translatableKey(String translatableKey) {
            this.translatableKey = translatableKey;
            return this;
        }

        public Builder lootTable(ResourceLocation lootTable) {
            this.lootTable = lootTable;
            return this;
        }

        public Builder successCondition(float[] successCondition) {
            this.successCondition = successCondition;
            return this;
        }

        public Builder weight(int weight) {
            this.weight = weight;
            return this;
        }

        public Builder stop() {
            this.resultType = ResultType.STOP;
            return this;
        }

        public Builder attributeBonus(float[] attributeBonus) {
            this.attributeBonus = attributeBonus;
            return this;
        }
    }

    // TODO 完成事件结果的解析
    public static List<VoyageResult> createFromJson(JsonElement jsonElement) {
        List<VoyageResult> results = new ArrayList<>();

        if (jsonElement.isJsonObject()) {
            Builder builder = new VoyageResult.Builder();
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            builder.translatableKey(GsonHelper.getAsString(jsonObject, "name"));
            builder.lootTable(new ResourceLocation(GsonHelper.getAsString(jsonObject, "lootTable")));

            results.add(new VoyageResult(builder));
        } else if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            for (var obj : jsonArray) {
                Builder builder = new VoyageResult.Builder();
                JsonObject jsonObject = obj.getAsJsonObject();
                builder.translatableKey(GsonHelper.getAsString(jsonObject, "name"));
                builder.lootTable(new ResourceLocation(GsonHelper.getAsString(jsonObject, "lootTable")));

                results.add(new VoyageResult(builder));
            }
        }

        return results;
    }
}
